--
-- Author: shunguo.chen
-- Date: 2018-04-03 16:11:57
--
-- CosKOFController
--

local CosKOFView = import("..views.CosKOFView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("CosplayModel")

end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:listEvent()
    return {
        "onMapChanged",
        "onReconnectSuccessed"
    }
end

function ClassRef:onEvent(eventName, ...)
    local data = ...
    if eventName == "onMapChanged" then
        -- data = 是否刚刚进入游戏
        if not data then
            self._model:reqStartKOF()
        end
    elseif eventName == "onReconnectSuccessed" then
        self:onReconnect()
    end
end

function ClassRef:openView(data)
    if cc.isDead(self._view) then
        self._view = CosKOFView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        self:addChild(self._view, data)
    end

    self._view:show()
    self._view:refresh(data)
end
function ClassRef:clearView()
    if self._view then
        self._view = nil

        -- 此处重连时关闭，可能infos不完整，就不需要再显示VS界面了
        local infos = self._model:getKOFVSInfos()
        if infos and #infos[1] == 3 and #infos[2] == 3 then
            scheduler.newCron(function()
                    app:sendMsg("PvpController", "openVSView", infos)
                end, 0.3)
        end
        self._model:clear()
    end
end

-------------------------------------------------------
-- 重连之后，有可能已经完成选择，统一关闭界面
function ClassRef:onReconnect()
    self:closeView()
end

function ClassRef:onUpdateSelect(info)
    if not self:isViewAlive() then return end
    self._view:updateInfo(info:isMyTeam())
end

function ClassRef:onSelectRole()
    if not self:isViewAlive() then return end
    self._view:playSelectAction()
end

-------------------------------------------------------
function ClassRef:onOperate(op, data)
    -- print("CosKOFController:onOperate op = " .. op)
    if op == "selectRole" then
        self._model:reqSelectRole(data)
    elseif op == "openRole" then
        app:sendMsg("CosplayController", "openBookView", data)
    end
end

return ClassRef
